#include "GUI/Gtk/gtkguiimpl.hpp"

#include "image.h"
#include "log.h"
#include "Frame/states.hpp"

GtkGUIImpl *GtkGUIImpl::_guiimpl = 0;

void GtkGUIImpl::m_Start()
{
	States *state = GtkGUIImpl::_guiimpl->m_state;
	if (state->RunningFlag)
	{
		Log("终止");
		GtkGUIImpl::_guiimpl->Stop();
	}
	else
	{
		Log("开始");
		GtkGUIImpl::_guiimpl->Start();
		Log("结束");
	}
}

void GtkGUIImpl::m_Quit()
{
	Log("终止");
	GtkGUIImpl::_guiimpl->Stop();
	Log("退出");
	gtk_main_quit();
}
	
void m_Pause()
{
	States *state = GtkGUIImpl::_guiimpl->m_state;
	if(state->RunningFlag)
	{
		state->PausingFlag = !state->PausingFlag;
		if (state->PausingFlag)
			Log("暂停");
		else
			Log("继续");
	}
}

void GtkGUIImpl::on_menu_activate(GtkWidget* item, gpointer data)
{
   g_print("menuitem %s is pressed.\n",(gchar*)data);
}

GtkGUIImpl::GtkGUIImpl()
{
	_guiimpl = this;
}

int GtkGUIImpl::Init(int argc, char** argv)
{
	gtk_init (&argc, &argv);
	return -1;
}

int GtkGUIImpl::MainLoop()
{
	gtk_widget_show_all(m_window);
	gtk_main();
	return -1;
}

int GtkGUIImpl::CreateWindow()
{
	m_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(m_window),GTK_WIN_POS_CENTER);
	
	gtk_signal_connect(GTK_OBJECT(m_window), "delete_event", GTK_SIGNAL_FUNC(m_Quit), NULL);
	
	m_main_vbox = gtk_vbox_new(FALSE,0);
	gtk_container_add(GTK_CONTAINER(m_window), m_main_vbox);
	
	//CreateMenu();
	CreateImage();
	CreateControlPanel();
	return -1;
}

void GtkGUIImpl::Update(const Image& image)
{
	GdkPixbuf* pix = gdk_pixbuf_new_from_data ((guchar*)image.data,
                 GDK_COLORSPACE_RGB,
                 FALSE,
				 8, image.width, image.height, image.widthStep, NULL, NULL);
	gtk_image_set_from_pixbuf((GtkImage*)m_image, pix);
}

void GtkGUIImpl::DoEvents(int s)
{
	gtk_main_iteration();
}
	
void GtkGUIImpl::CreateImage()
{
	m_image=gtk_image_new_from_file("data/test.jpg"); 
	gtk_box_pack_start(GTK_BOX(m_main_vbox), m_image, false, false, 0);
}

void GtkGUIImpl::CreateControlPanel()
{
	m_control_hbox = gtk_hbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(m_main_vbox), m_control_hbox, false, false, 0);
	
	//GtkWidget* m_button_start, m_button_pause, m_button_stop;
	GtkWidget *m_button_start = gtk_button_new_with_label ("开始");
	gtk_signal_connect(GTK_OBJECT(m_button_start), "clicked", GTK_SIGNAL_FUNC(m_Start), GTK_OBJECT(m_image));
	gtk_box_pack_start(GTK_BOX(m_control_hbox), m_button_start, false, false, 0);
	
	GtkWidget *m_button_pause = gtk_button_new_with_label ("暂停");
	gtk_signal_connect(GTK_OBJECT(m_button_pause), "clicked", GTK_SIGNAL_FUNC(m_Pause), GTK_OBJECT(m_image));
	gtk_box_pack_start(GTK_BOX(m_control_hbox),m_button_pause, false, false, 0);
}
void GtkGUIImpl::CreateMenu()
{
	GtkWidget* menubar;
	GtkWidget* menu;
	GtkWidget* editmenu;
	GtkWidget* helpmenu;
	GtkWidget* rootmenu;
	GtkWidget* menuitem;
	GtkAccelGroup* accel_group;
	
	accel_group=gtk_accel_group_new();
	gtk_window_add_accel_group(GTK_WINDOW(m_window),accel_group);
	
	menu=gtk_menu_new();
	menubar=gtk_menu_bar_new();
	
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 新建")); 
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 打开"));
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 保存"));
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE_AS,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 另存为"));
	menuitem=gtk_separator_menu_item_new();
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	menuitem=gtk_image_menu_item_new_from_stock( GTK_STOCK_QUIT,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(m_Quit),(gpointer)("退出"));
	rootmenu=gtk_menu_item_new_with_label(" 文件 ");
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),menu);
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu); 
	

	editmenu=gtk_menu_new();
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_CUT,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 剪切 "));
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY,accel_group); 
	gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)("复制 "));
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 粘贴 ")); 
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_FIND,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(editmenu),menuitem); 
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 查找 "));
	
	rootmenu=gtk_menu_item_new_with_label(" 编辑 ");
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),editmenu);
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
	

	helpmenu=gtk_menu_new();
	menuitem=gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP,accel_group);
	gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)( " 帮助 "));
	menuitem=gtk_menu_item_new_with_label(" 关于...");
	gtk_menu_shell_append(GTK_MENU_SHELL(helpmenu),menuitem);
	g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_menu_activate),(gpointer)(" 关于 "));
	
	rootmenu=gtk_menu_item_new_with_label(" 帮助 ");	
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(rootmenu),helpmenu);
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar),rootmenu);
	
	gtk_box_pack_start(GTK_BOX(m_main_vbox),menubar, false, false, 0);
}
